/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.andkulikov.transitionseverywhere.slice;

import com.transitionseverywhere.ChangeText;
import com.andkulikov.transitionseverywhere.ResourceTable;
import com.transitionseverywhere.TransitionManager;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.utils.LightweightMap;

public class ChangeTextSlice extends AbilitySlice implements Component.ClickedListener {
    public static final String TEXT_1 = "Hi, i am text. Tap on me!";
    private boolean mSecondText;
    private StackLayout mSlViewRoot;
    private Text mTxtTransition;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer layout = (ComponentContainer) LayoutScatter.getInstance(this).
                parse(ResourceTable.Layout_slice_change_text, null, false);
        mSlViewRoot = (StackLayout) layout.findComponentById(ResourceTable.Id_slViewRoot);
        Object tagMap = new LightweightMap<>();
        mSlViewRoot.setTag(tagMap);
        mTxtTransition = (Text) layout.findComponentById(ResourceTable.Id_txtTransition);
        mTxtTransition.setText(TEXT_1);
        mTxtTransition.setTextColor(Color.BLUE);
        mTxtTransition.setClickedListener(this::onClick);
        super.setUIContent(layout);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_txtTransition:
                mSecondText = !mSecondText;
                TransitionManager.beginDelayedTransition(mSlViewRoot,
                        new ChangeText().setChangeBehavior(ChangeText.CHANGE_BEHAVIOR_OUT_IN));
                break;
        }
    }
}
